#!coding=utf-8
from standard_function import *

def WaitforElementByContentDesc(dr,desc,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_element_by_accessibility_id(desc)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            return element

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementByContentDesc(driver,u"下载管理",30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
       btn = WaitforElementByName(driver,u'下载管理',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
       btn = WaitforElementById(driver,'com.coolapk.market:id/delete_view',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'android:id/button1',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()
    driver.back()

def clickappdown(driver):
    try:
        btns=WaitforElementsById(driver,'com.coolapk.market:id/action_button',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        for btn in btns:
            btn.click()
            try:
                right=WaitforElementById(driver,'android:id/button1',5)
            except NoSuchElementException:
                pass
            else:
                right.click()
            waittime=20
            while waittime:
                text=btn.get_attribute("text")
                print text
                if '0%' not in text:
                    btn.click()
                    break
                else:
                    time.sleep(0.5)
                    if waittime==0:
                        btn.click()
                        break
                    waittime=waittime-1
            print "----------------------------------------------"
    #driver.back()



def soft(driver):
    categorys = [u'系统工具',u'桌面插件',u'主题美化',u'社交聊天',u'咨询阅读',u'通讯网络',u'影音娱乐',u'摄影图片',u'生活服务',u'实用工具',u'文档商务',u'金融财经',u'运动健康',u'学习教育', \
                 u'旅行交通',u'购物',u'Xposed模块',u'VR',u'其他']
    try:
        WaitforElementByName(driver,u'分类',30).click()
        WaitforElementByName(driver,u"应用分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def game(driver):
    categorys = [u'二次元及周边',u'休闲',u'益智',u'棋牌',u'体育',u'竞速',u'动作',u'射击',u'掌上网游',u'策略塔防',u'角色扮演',u'模拟经营',u'汉化移植',u'冒险',u'音乐游戏',u'游戏辅助',u'其他']
    try:
        WaitforElementByName(driver,u'分类',30).click()
        WaitforElementByName(driver,u"游戏分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.coolapk.market'    # apk的包名
    desired_caps['appActivity'] = '.activity.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #driver.find_element_by_name().tag_name
    del_appdown_task(driver)
    soft(driver)
    time.sleep(2)
    game(driver)
    driver.quit()

#run()